package com.yang.leather.core;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.yang.leather.common.hibernate.BaseDaoImpl;
import com.yang.leather.common.hibernate.Finder;
import com.yang.leather.common.page.Pagination;

public class LeatherCoreDaoImpl<T extends Serializable> extends BaseDaoImpl<T> implements LeatherCoreDao<T> {

	/**
	 * 
	 * @param hql 
	 * @param param 参数
	 * @param pageNo 当前页
	 * @param pageSize 每页大小
	 * @param assembleCondition 是否组装条件
	 * @return
	 */
	public Pagination pagination(String hql, Map<String,Object> param, int pageNo, int pageSize, boolean assembleCondition) {
		Finder f = Finder.create(hql);
		
		createFinder(f,param,assembleCondition);
		
		return find(f, pageNo, pageSize);
	}

	public int count(String hql, Map<String,Object> param, boolean assembleCondition){
		Finder f = Finder.create(hql);
		
		createFinder(f,param,assembleCondition);
		
		return countQueryResult(f);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> getList(String hql,Map<String,Object> param, boolean assembleCondition){
		Finder f = Finder.create(hql);
		
		createFinder(f,param,assembleCondition);
		
		return find(f);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> getLimitList(String hql,Map<String,Object> param,int limit, boolean assembleCondition){
		Finder f = Finder.create(hql);
		f.setMaxResults(limit);
		
		createFinder(f,param,assembleCondition);
		
		return find(f);
	}
	
	@SuppressWarnings("unchecked")
	private void createFinder(Finder f, Map<String,Object> param, boolean assembleCondition){
		if(param!=null && param.size()>0){
			if(assembleCondition){
				String likeName = null;
				for(Map.Entry<String, Object> entry : param.entrySet()){
					if(entry.getValue()!=null){
						if("cell".equals(entry.getKey()) || "orderBy".equals(entry.getKey())){
							continue;
						}
						likeName = entry.getKey().replaceAll("[.]", "");
						if(entry.getValue() instanceof String){
							f.append(" and " + entry.getKey() + " like :" + likeName);
							f.setParam(entry.getKey(), entry.getValue());
						}else if(entry.getValue() instanceof Long){
							f.append(" and " + entry.getKey() + " = :" + likeName);
							f.setParam(entry.getKey(),entry.getValue());
						}else if(entry.getValue() instanceof Integer){
							f.append(" and " + entry.getKey() + " = :" + likeName);
							f.setParam(entry.getKey(),entry.getValue());
						}else if(entry.getValue() instanceof Collection){
							f.append(" and " + entry.getKey() + " in :" + likeName);
							f.setParamList(entry.getKey(), (Collection)entry.getValue());
						}
					}
				}
				
				if(param.get("cell")!=null && param.get("orderBy")!=null){
					f.append(" order by ")
						.append(param.get("cell").toString())
						.append(" ")
						.append(param.get("orderBy").toString());
				}
			}else{
				for(Map.Entry<String, Object> entry : param.entrySet()){
					if(entry.getValue()!=null){
						if(entry.getValue() instanceof String){
							f.setParam(entry.getKey(), entry.getValue());
						}else if(entry.getValue() instanceof Long){
							f.setParam(entry.getKey(),entry.getValue());
						}else if(entry.getValue() instanceof Integer){
							f.setParam(entry.getKey(),entry.getValue());
						}else if(entry.getValue() instanceof Collection){
							f.setParamList(entry.getKey(), (Collection)entry.getValue());
						}
					}
				}
			}
		}
	}
}
